#ifndef UTILS_H
#define UTILS_H

#include <QVector>
#include <QList>
#include <QFile>
#include <QSet>

//struct Range{
//    qint64 start;
//    qint64 end;
//    Range(){}
//    Range(qint64 s, qint64 e) : start(s), end(e){ Q_ASSERT(Valid()); }
//    qint64 Size() const { Q_ASSERT(Valid()); return end - start; }
//    bool Valid() const { return end > start; }
//    bool Empty() const { return end == start; }
//};

struct Line {
    qint64 start;
    qint64 size;
    Line(){} Line(qint64 st, qint64 sz) : start(st), size(sz) { }
    bool operator == (const Line &b) const {
        return start == b.start;
    }
    bool operator < (const Line &b) const {
        return start < b.start;
    }
};

inline uint qHash(const Line & line)
{
    return line.start;
}

class Utils
{
public:
    static void cross(const QVector<Line> & vec, QSet<Line> & result);
    static bool readLine(QFile & file, QString & line, qint64 & readBytes);
};

inline QString prettyDateTime(const QString & text) {
    int yearStart = 1970, yearEnd = 2050;
    QString result;
    for (int i=yearStart; i<=yearEnd; ++i) {
        int idx = text.indexOf(QString::number(i));
        if (idx != -1) {
            auto date = text.midRef(idx, 14);
            if (date.toULongLong() == 0) {
                date = text.midRef(idx, 12);
            }
            if (date.toULongLong() != 0) {
                result.reserve(16);
                result.append(date.mid(0, 4));
                result.append("-");
                result.append(date.mid(4, 2));
                result.append("-");
                result.append(date.mid(6, 2));
                result.append(" ");
                result.append(date.mid(8, 2));
                result.append(":");
                result.append(date.mid(10, 2));
                if (date.size() == 14) {
                    result.append(":");
                    result.append(date.mid(12, 2));
                }
                return result;
            }
        }
    }
    return text;
}

inline QString commaNumber(long int number) {
    auto result = QString::number(number);
    for (int i=result.size()-3; i>0; i-=3) {
        result.insert(i, ',');
    }
    return result;
}

#endif // UTILS_H
